home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / dejagnu.lha / dejagnu-1.0.1 / expect / lib_string.c < prev    next >
Text File  |  1993-03-02  |  3KB  |  133 lines

  1. /* lib_stringmatch.c - stolen from Tcl.  Thanks, John. */
  2. /* Author: John Ousterhout, ouster@sprite.berkeley.edu */
  3. /*
  4.  *----------------------------------------------------------------------
  5.  *
  6.  * exp_stringmatch --
  7.  *
  8.  *    See if a particular string matches a particular pattern.
  9.  *
  10.  * Results:
  11.  *    The return value is 1 if string matches pattern, and
  12.  *    0 otherwise.  The matching operation permits the following
  13.  *    special characters in the pattern: *?\[] (see the manual
  14.  *    entry for details on what these mean).
  15.  *
  16.  * Side effects:
  17.  *    None.
  18.  *
  19.  *----------------------------------------------------------------------
  20.  */
  21.  
  22. int
  23. exp_stringmatch(string, pattern)
  24.     register char *string;    /* String. */
  25.     register char *pattern;    /* Pattern, which may contain
  26.                  * special characters. */
  27. {
  28.     char c2;
  29.  
  30.     while (1) {
  31.     /* See if we're at the end of both the pattern and the string.
  32.      * If so, we succeeded.  If we're at the end of the pattern
  33.      * but not at the end of the string, we failed.
  34.      */
  35.     
  36.     if (*pattern == 0) {
  37.         if (*string == 0) {
  38.         return 1;
  39.         } else {
  40.         return 0;
  41.         }
  42.     }
  43.     if ((*string == 0) && (*pattern != '*')) {
  44.         return 0;
  45.     }
  46.  
  47.     /* Check for a "*" as the next pattern character.  It matches
  48.      * any substring.  We handle this by calling ourselves
  49.      * recursively for each postfix of string, until either we
  50.      * match or we reach the end of the string.
  51.      */
  52.     
  53.     if (*pattern == '*') {
  54.         pattern += 1;
  55.         if (*pattern == 0) {
  56.         return 1;
  57.         }
  58.         while (*string != 0) {
  59.         if (exp_stringmatch(string, pattern)) {
  60.             return 1;
  61.         }
  62.         string += 1;
  63.         }
  64.         return 0;
  65.     }
  66.     
  67.     /* Check for a "?" as the next pattern character.  It matches
  68.      * any single character.
  69.      */
  70.  
  71.     if (*pattern == '?') {
  72.         goto thisCharOK;
  73.     }
  74.  
  75.     /* Check for a "[" as the next pattern character.  It is followed
  76.      * by a list of characters that are acceptable, or by a range
  77.      * (two characters separated by "-").
  78.      */
  79.     
  80.     if (*pattern == '[') {
  81.         pattern += 1;
  82.         while (1) {
  83.         if ((*pattern == ']') || (*pattern == 0)) {
  84.             return 0;
  85.         }
  86.         if (*pattern == *string) {
  87.             break;
  88.         }
  89.         if (pattern[1] == '-') {
  90.             c2 = pattern[2];
  91.             if (c2 == 0) {
  92.             return 0;
  93.             }
  94.             if ((*pattern <= *string) && (c2 >= *string)) {
  95.             break;
  96.             }
  97.             if ((*pattern >= *string) && (c2 <= *string)) {
  98.             break;
  99.             }
  100.             pattern += 2;
  101.         }
  102.         pattern += 1;
  103.         }
  104.         while ((*pattern != ']') && (*pattern != 0)) {
  105.         pattern += 1;
  106.         }
  107.         goto thisCharOK;
  108.     }
  109.     
  110.     /* If the next pattern character is backslash, just strip it off
  111.      * so we do exact matching on the character that follows.
  112.      */
  113.     
  114.     if (*pattern == '\\') {
  115.         pattern += 1;
  116.         if (*pattern == 0) {
  117.         return 0;
  118.         }
  119.     }
  120.  
  121.     /* There's no special character.  Just make sure that the next
  122.      * characters of each string match.
  123.      */
  124.     
  125.     if (*pattern != *string) {
  126.         return 0;
  127.     }
  128.  
  129.     thisCharOK: pattern += 1;
  130.     string += 1;
  131.     }
  132. }
  133.